Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be

Size: px
Start display at page:

Download "Arrays. ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be"

Transcription

1 Arrays ˆ An array, is a linear data structure consisting of a collection of elements, each identified by one array index. ˆ For math, arrays could be ˆ row vectors: u R 1 n for any positive integer n ˆ column vectors: u R n 1 for any positive integer n ˆ matrices: A R n m for any positive integers n, m ˆ Arrays can be said the simplest one of data structures. Zheng-Liang Lu 55 / 124

2 Example 1 >> rowvector = [1 2 3] % [... ] is used in arrays 2 3 rowvector = >> colvector = [1; 2; 3] % semicolon used to... change rows colvector = Zheng-Liang Lu 56 / 124

3 17 18 >> A = [1 2 3; 4 5 6; 7 8 9] A = ˆ You can create an n m k matrix for n, m, k,... N. ˆ For example, a matrix takes MBytes in memory. 1 ˆ The number of dimensions of arrays is unlimited without regarding to the limit of the memory space. 1 More explicit, / 2 20 = Zheng-Liang Lu 57 / 124

4 Alternatives for Vector Creation ˆ The function linspace(start, end, numpoints) generates a vector of uniformly incremented values. 2 1 >> x = linspace(0, 10, 5) 2 3 x = Also try logspace. Zheng-Liang Lu 58 / 124

5 ˆ You can also use the colon notation, which is to create a vector. start : stepsize : end 1 >> x = 0 : 2 : x = Zheng-Liang Lu 59 / 124

6 ˆ This is widely used to create index arrays to manipulate data arrays. 1 >> x = 1 : 10 % default step size = x = ˆ You cannot create a vector by a contradiction. 1 >> x = 1 : -1 : x = 4 5 Empty matrix: 1-by-0 Zheng-Liang Lu 60 / 124

7 ˆ Some special matrices can be initialized by the following functions: Zheng-Liang Lu 61 / 124

8 Array Addressing ˆ Subscripted indexing ˆ Linear indexing: column major order. 1 >> A(2, 3) % using subscripted indexing 2 3 ans = >> A(8) % using linear indexing 8 9 ans = Zheng-Liang Lu 62 / 124

9 1 >> A(2, [1 3]) % selecting 2 3 ans = >> A(:, 1) = [] % deleting the first column vector A = >> A = [[1; 4; 7], A] % combining two matrices ˆ In Line 7, we can use the colon operator (:) to select all elements in the 1st column. Zheng-Liang Lu 63 / 124

10 Cells and Cell Arrays 3 ˆ An array cannot contain data of different types correctly! ˆ A cell array is a data type with indexed data containers called cells, and each cell can contain any type of data. ˆ Cell arrays commonly contain either lists of text strings, combinations of text and numbers, or numeric arrays of different sizes. 3 Basically, cell arrays do not require completely contiguous memory because Matlab does not allocate any memory for the contents of each cell when the cell array is created. However, each cell requires contiguous memory, as does the cell array header that Matlab creates to describe the array. Zheng-Liang Lu 64 / 124

11 Example 4 1 >> A{1} = 'first cell'; 2 >> A{2} = [1 2 3; 4 5 6; 7 8 9]; 3 >> A{3} = {'Tim'; 'Chris'}; 4 >> A 5 6 A = 7 8 'first cell' [3x3 double] {2x1 cell} ˆ However, the arithmetic operators cannot be applied to the cell arrays! 4 Thanks to a lively class discussion (Matlab263) on March 3, Zheng-Liang Lu 65 / 124

12 Referring to Cell Arrays ˆ Using curly braces, { } will reference the contents of a cell. 1 >> A{3}{1} 2 3 ans = 4 5 Tim ˆ More details can be found in here. Zheng-Liang Lu 66 / 124

13 Structures and Structure Arrays ˆ A structure groups related data by fields, which can contain any type of data. ˆ You can access data of the structure by the period operator. ˆ For example, 1 >> Student.university = 'NTU'; 2 >> Student.department = 'csie'; 3 >> Student.grades = [80, 90, 100]; ˆ More details can be found here. ˆ Widely used in object-oriented applications, such as GUI and databases. ˆ The arithmetic operators cannot be applied to structures, either! Zheng-Liang Lu 67 / 124

14 Symbols for Common Data Structures Zheng-Liang Lu 68 / 124

15 Summary 5,6 ˆ Parentheses ( ) ˆ Arithmetic, e.g. (x + y)/z. ˆ Input arguments of functions, e.g. sin(1), exp(1). ˆ Array addressing, e.g. A(1) refers to the first element in array A. ˆ Square brackets [ ]: only used in array operations ˆ e.g. x = [ ]. ˆ Curly brackets { }: only used in cells ˆ e.g. A = { This is Matlab class., x}. 5 Thanks to a lively class discussion (Matlab237) on April 16, You can refer to this page for more details of special characters. Zheng-Liang Lu 69 / 124

16 More Data Structures ˆ See data-types_data-types.html. 7 7 Some of them may not be available if your version is out-of-date. Zheng-Liang Lu 70 / 124

17 Vectorization 8 ˆ Matlab favors array operations. ˆ When two arrays have the same dimensions, addition, subtraction, multiplication, and division apply on an element-by-element basis. ˆ For example, 1 >> x = [1 2 3]; 2 >> y = [4 5 6]; 3 >> x + y 4 5 ans = More about vectorization. Zheng-Liang Lu 71 / 124

18 Element-By-Element Operations ˆ We will see the difference between left and right divisions in matrix computation. Zheng-Liang Lu 72 / 124

19 1 >> Lecture 2 2 >> 3 >> -- Programming Basics 4 >> Zheng-Liang Lu 73 / 124

20 If debugging is the process of removing software bugs, then programming must be the process of putting them in. Edsger W. Dijkstra ( ) Zheng-Liang Lu 74 / 124

21 Introduction ˆ Decision making facilitates the usefulness of programs. ˆ For example, Self Driving Cars by Google. 9 ˆ Also, some actions (functions) are repeated for a specified number of times or until the stopping condition is satisfied. ˆ An algorithm must have the ability to alter the order of its instructions using what is called a control structure. ˆ The first principle in designing algorithms is to divide and conquer. ˆ Recall the algorithm for max(a). ˆ This feature enables engineers to solve problems of great complexity or requiring numerous calculations. 9 See Zheng-Liang Lu 75 / 124

22 Building Blocks ˆ Sequential operations: be executed in order. ˆ Selections: check which condition is satisfied and then execute the actions accordingly. ˆ Repetitions: repeat some instructions and stop while the termination condition is satisfied. Zheng-Liang Lu 76 / 124

23 Pseudo Code ˆ The pseudo code is widely used to describe algorithms. ˆ They are a mixture of natural language and mathematical expressions with common structures of programming languages. ˆ For example, 1 if one student's grade is greater than 60 2 print 'pass' 3 else 4 print 'fail' 5 end Zheng-Liang Lu 77 / 124

24 Relational Operators 10 ˆ Matlab provides 6 relational operators to make comparisons between two arrays of equal size. 10 See Table 8.1 in Moore, p Zheng-Liang Lu 78 / 124

25 Boolean (Logical) Values ˆ Boolean values are only true and false. 11 ˆ The function true and false represent logical true and logical false, respectively. 12 ˆ Boolean variable contains a boolean value. ˆ For example, 1 >> x = 1; y = 2; 2 >> w = (y > x) ~= true 3 4 w = Note that the numeric number 0 can be regarded as false in general, and nonzero numbers are regarded as true in Matlab and some programming languages. 12 The usage of true and false is similar to zeros. Zheng-Liang Lu 79 / 124

26 Mask: A Filter ˆ Boolean arrays are often used as masks to manipulate arrays as an alternative to index arrays 1 >> scores = 50 : 10 : 70; 2 >> haspassed = scores >= haspassed = >> scores(haspassed) 9 10 ans = Zheng-Liang Lu 80 / 124

27 Logical Operators ˆ Assume x = 0; ˆ How about 1 < x < 3? (Surprising!) Zheng-Liang Lu 81 / 124

28 Truth Table ˆ Let A and B be two Boolean variables. ˆ Then the truth table for logical operators is as follows: A B A A&B A B xor(a, B) T T F T T F T F F F T T F T T F T T F F T F F F ˆ Note that the instructions of computers, such as arithmetic operations, are implemented by logic gates See any textbook for digital circuit design. Zheng-Liang Lu 82 / 124

29 & vs. == 14 1 >> x = [ ]; 2 >> y = [ ]; 3 >> x == y 4 5 ans = >> x & y 11 ans = Thanks to a lively class discussion (Matlab-237) on April 16, Zheng-Liang Lu 83 / 124

30 Quantifiers 15 ˆ The function all determines if all elements are true. 1 >> x = [ ]; 2 >> all(x >= 60) 3 ans = ˆ The function any determines if there is any true element in the array. 1 >> any(x >= 60) 2 ans = See Zheng-Liang Lu 84 / 124

31 Precedence of Operators See Table 1.2 Operator Precedence Rules in Attaway, p. 25. Zheng-Liang Lu 85 / 124

32 More Logical Functions ˆ NaN: Not A Number, caused by and See NaN. Zheng-Liang Lu 86 / 124

33 Digress: Set Membership 19 ˆ Matlab provides useful functions such as intersect, union, unique, and setdiff. 18 ˆ For example, 1 >> x = [ ]; y = [ ]; 2 >> intersect(x, y) 3 4 ans = See 19 Thanks to a lively class discussion (Matlab263) on March 9, Zheng-Liang Lu 87 / 124

34 Logic is the anatomy of thought. John Locke ( ) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than any other person because he does not imagine that he knows what he does not know.) Zheng-Liang Lu 88 / 124

35 Selections ˆ Selection enables us to write programs that make decisions on. ˆ Selection structures contain one or more of the if, else, and elseif statements. ˆ The end statement denotes the end of selection structures. Zheng-Liang Lu 89 / 124

36 Example: if-else ˆ If x is a nonnegative real number, then y = x. ˆ The function input(prompt) shows the prompt (in a string) and then waits for a numeric input from the keyboard. 1 clear; clc; 2 3 x = input('enter a number? '); % input from keyboard 4 if x >= 0 5 sqrt(x) 6 else 7 disp([num2str(x),' is a negative number.']); 8 end ˆ Note that one if should be paired with one end. ˆ input(prompt, s ) waits for a string input. Zheng-Liang Lu 90 / 124

37 Exercise: Nested Conditional Statements 1 clear; clc; 2 x = input('enter x?', 's'); 3 % Contribution by Mr. Curtis Yen on Sep. 10, w = str2num(x); 5 if isempty(w) % w is not empty if w is a number 6 disp([x, ' is not a number.']); 7 else 8 if w >= 0 9 sqrt(w) 10 else 11 disp([x ' is not a positive real number.']); 12 end 13 end ˆ Do not write a deeply nested conditional statement. Zheng-Liang Lu 91 / 124

38 Example: if-elseif-else 1 clear; clc; 2 x = input('enter x?', 's'); 3 w = str2num(x); 4 if isempty(w) % w is not empty if w is a number 5 disp([x ' is not a number.']); 6 elseif w >= 0 7 sqrt(w) 8 else 9 disp([x ' is not a positive real number.']); 10 end ˆ Can we change the order of the conditions? ˆ You can put w >= 0 in the first condition. 20 ˆ The empty set can be evaluated as false by the if statement. 20 Thanks to a lively class discussion (Matlab-260) on August 29, Zheng-Liang Lu 92 / 124

39 Example: GPA Conversion Problem 21 ˆ Write a program which converts centesimal points to GPA. ˆ Let x be the grade input. ˆ For simplicity, ˆ If 90 x 100, then x is converted to 4. ˆ If 80 x < 90, then 3. ˆ If 70 x < 80, then 2. ˆ If 60 x < 70, then 1. ˆ If x < 60, then See GPA. Zheng-Liang Lu 93 / 124

40 1 clear; clc; 2 x = input('enter points? '); 3 if x >= 90 && x <= disp('4'); 5 elseif x >= 80 && x < 90 6 disp('3'); 7 elseif x >= 70 && x < 80 8 disp('2'); 9 elseif x >= 60 && x < disp('1'); 11 else 12 disp('0'); 13 end ˆ Can we reverse the conditions? ˆ Can we place the conditions at your will? Zheng-Liang Lu 94 / 124

41 & vs. && 1 clear; clc; 2 3 x = [ ]; 4 y = [ ]; 5 6 x > 0 & y > 0 % return [ ] 7 all(x > 0) && any(y > 0) % return 0 ˆ Consider A && B. ˆ If A returns false, then B won t be evaluated. ˆ This is so-called the short-circuit operator, which facilitates time-saving. ˆ Note that the argument for if statements can be a scalar only! Zheng-Liang Lu 95 / 124

42 Another Selection Structure: switch-case ˆ The switch-case statements allow you to choose between multiple outcomes by checking the value of the target variable. ˆ For example, 1 clear; clc; 2 city = input('enter the name of a city: ', 's'); 3 switch city 4 case {'Taipei', 'New Taipei'} 5 disp('price: $100'); 6 case 'Taichung' 7 disp('price: $200'); 8 case 'Tainan' 9 disp('price: $300'); 10 otherwise 11 disp('not an option.'); % default option 12 end Zheng-Liang Lu 96 / 124

43 Why Not if? if strcmp(city, 'Taipei') strcmp(city, 'New... Taipei') 3 disp('price: $100'); 4 elseif strcmp(city, 'Taichung') 5 disp('price: $200'); 6 elseif strcmp(city, 'Tainan') 7 disp('price: $300'); 8 else 9 disp('not an option.'); 10 end ˆ Can you implement the strcmp function? 22 Thanks to a lively class discussion (Matlab-244) on August 20, Zheng-Liang Lu 97 / 124

44 Exercise ˆ We can try to replicate the functionality of strcmp by not calling it. ˆ You may use the function length to calculate the size of the string (or any arrays later) istaipei = length(city) == length('taipei') &&... 3 all(city == 'Taipei'); 4 isnewtaipei =... % Do the same thing below. 5 if istaipei isnewtaipei 6... % Show the ticket price. 7 elseif % Do the same thing below. 9 end Zheng-Liang Lu 98 / 124

45 Error and Error Handling ˆ You can issue an error if you do not allow the callee for some situations >> error('this is an error.'); ˆ Also, you can use a try-catch statement to handle errors. 1 try 2 % normal operations 3 catch 4 % handler operations 5 end 23 In other words, you can decide if the action can be performed. Zheng-Liang Lu 99 / 124

46 Example: Combinations ˆ For all nonnegative integers n k, ( n k) is given by ( ) n n! = k k!(n k)!. ˆ Note that factorial(n) returns n!. 1 clear; clc; 2 3 n = input('n =? '); 4 k = input('k =? '); 5 y = factorial(n) / (factorial(k) * factorial(n - k)) 6 disp('end of program.'); Zheng-Liang Lu 100 / 124

47 ˆ Try n = 2, k = 5. ˆ factorial( 3) is not allowed! ˆ So your program stops in Line 5, and terminates abnormally. ˆ The reason is that Line 5 produces an error and the program is not designed to handle it. ˆ Then Matlab takes over and kills this program try 3 y = factorial(n) / (factorial(k) *... factorial(n - k)) 4 catch e % capture the thrown exception 5 disp(['error: ' e.message]); 6 end 7 disp('end of program.'); Zheng-Liang Lu 101 / 124

48 Exercise: Divided by Zero ˆ Write a program which calculates x/y for any two real numbers x, y. ˆ The program restricts the user to do division by zero. ˆ Note that we don t use this error-handling mechanism for normal executions in regard to the performance! 1... % assume x = 1 any y = 0 2 if y == 0 3 error('divided by zero!'); 4 else 5 ans = x / y; 6 end 7... Zheng-Liang Lu 102 / 124

49 Repetitions ˆ If a group of instructions is potentially repeated, you should wrap those in a repetition structure, called loops. ˆ All loops consist of 3 parts: ˆ Find the repeated pattern for each iteration. ˆ Warp them by a loop. ˆ Set the continuation condition by defining a loop variable with some criterion. ˆ Matlab provides two different types: the for loops and the while loops. ˆ Use for loops if you know the number of iterations. ˆ Otherwise, use while loops. Zheng-Liang Lu 103 / 124

50 for Loops ˆ A for loop is the easiest choice when you know how many times you need to repeat the loop. 1 for loopvar = somearray 2 % body 3 end ˆ Particularly, we often use for loops to manipulate arrays! Zheng-Liang Lu 104 / 124

51 Zheng-Liang Lu 105 / 124

52 Example 1 for i = 1 : 10 2 disp(i); 3 end ˆ Can you show the odd integers from 1 to 9? 1 for student = {'Arthur', 'Alice'} 2 disp(cell2mat(student)); 3 % remember to take things out of cells 4 end ˆ Clearly, Matlab has for-each loops, which is an enhanced one compared to the naive one in C. ˆ Note that we use the function cell2mat so that a string stored in the cell can be extracted as a string. Zheng-Liang Lu 106 / 124

53 Example: Find Maximum (Revisited) 1 clear; clc; 2 3 x = [ ]; % input list 4 max = x(1); 5 for i = 2 : 7 6 if max < x(i) 7 max = x(i); 8 end 9 end 10 max ˆ Can you find the location of the maximum element? ˆ Try to find the minimum element and its location. Zheng-Liang Lu 107 / 124

54 Example: Running Sum ˆ Write a program which calculates the sum of array elements. 1 clear; clc; 2 3 x = 1 : 1 : 100; 4 s = 0; 5 for i = 1 : length(x) 6 s = s + x(i); % running sum 7 end 8 s ˆ Use length(x) instead of 100 in Line 5. ˆ Then the loop will do as many times as the number of elements in x. Zheng-Liang Lu 108 / 124

55 Exercise: Estimating π by Monte Carlo Simulation 24 ˆ Write a program which estimates π by generating a certain number of sample points (x, y) and calculating ˆπ = 4 m n, where m is the number of points falling in the region of the quarter circle shown in the next page, n is the total number of points, and ˆπ is the estimate of π. 24 Monte Carlo was a code word used in World War II to describe sampling-based methods for neutron scattering computations associated with Manhattan project. Zheng-Liang Lu 109 / 124

56 Zheng-Liang Lu 110 / 124

57 1 clear; clc; 2 3 n = 1e5; 4 m = 0; 5 for i = 1 : n 6 x = rand(1); 7 y = rand(1); 8 if x ˆ 2 + y ˆ 2 < 1 9 m = m + 1; 10 end 11 end 12 estimated pi = 4 * m / n ˆ ˆπ π as n by the law of large numbers (LLN). 25 ˆ Try to vectorize this program. 25 See Zheng-Liang Lu 111 / 124

58 More Exercises ˆ Write a program to estimate 5 by Monte Carlo. ˆ Write a program to estimate 1 0 xdx by Monte Carlo. ˆ Write a program to estimate the call price of European option by Monte Carlo. Zheng-Liang Lu 112 / 124

59 while Loops ˆ The while loops are preferred when you need to keep repeating the instructions until a continuation criterion is not met. 1 while criterion 2 % body 3 end ˆ Note that before entering the while loop, the criterion will be checked. ˆ Also note that the if statement is similar to the while loop except that the if statement executes only once. Zheng-Liang Lu 113 / 124

60 Zheng-Liang Lu 114 / 124

61 Example: Compounding ˆ Let x be the initial amount of some investment, and r be the annual interest rate. ˆ Write a program which calculates the holding years n so that this investment doubles it value. Zheng-Liang Lu 115 / 124

62 Solution ˆ In this case, we don t know how many iterations we need before the loop. 1 clear; clc; 2 3 curr = 100; 4 goal = 200; 5 r = 0.01; 6 n = 0; 7 8 while curr <= goal 9 curr = curr * (1 + r); 10 n = n + 1; 11 end 12 n ˆ Note that the criterion is to continue the loop. Zheng-Liang Lu 116 / 124

63 Infinite Loops 1 while true 2 disp('press ctrl+c to stop me!!!'); 3 end ˆ Note that your program can terminate the program by pressing ctrl+c. Zheng-Liang Lu 117 / 124

64 More Exercises ˆ Let a > b be two any positive integers. ˆ Write a program which calculates the remainder of a divided by b. ˆ Do not use mod(a, b). ˆ Write a program which determines the greatest common divisor (GCD) of a and b. ˆ Do not use gcd(a, b). Zheng-Liang Lu 118 / 124

65 Numerical Example: Bisection Method for Root-finding Zheng-Liang Lu 119 / 124

66 Problem Formulation Input - Consider f (x) = x 3 x 2 - Initial search interval [a, b] for any real numbers a < b - Error tolerance ɛ = 1e 9 Output - ˆr which approximates the exact solution r ˆ Note that a and b will be updated iteratively. Zheng-Liang Lu 120 / 124

67 Solution 1 clear; clc; 2 3 a = 0; b = 2; eps = 1e-9; 4 iter = 0; % the number of iterations 5 6 while b - a > eps 7 8 c = (a + b) / 2; 9 fa = a * a * a - a - 2; 10 fc = c * c * c - c - 2; if fa * fc < 0 13 b = c; 14 else 15 a = c; 16 end 17 Zheng-Liang Lu 121 / 124

68 18 iter = iter + 1; 19 end 20 r = c ˆ You may try another algorithm for the root finding problem, say the Newton-Raphson method. Zheng-Liang Lu 122 / 124

69 c = Zheng-Liang Lu 123 / 124

70 Exercise: Square Roots 26 ˆ Using the aforesaid bisection algorithm, write a function which determines n for all n R +. ˆ It is easy to see that n = x is equivalent to x 2 n = 0. ˆ So n is the positive root of x 2 n = 0, which is the input of the bisection algorithm. ˆ Trivially, a = 0. ˆ b = n if n > 1; b = 1, otherwise. 26 Also see Best-Square-Root-Method-Algorithm-Function-Precisi. Zheng-Liang Lu 124 / 124

Selections. Zheng-Liang Lu 91 / 120

Selections. Zheng-Liang Lu 91 / 120 Selections ˆ Selection enables us to write programs that make decisions on. ˆ Selection structures contain one or more of the if, else, and elseif statements. ˆ The end statement denotes the end of selection

More information

Variables and Assignments

Variables and Assignments Variables and Assignments ˆ A variable is used to keep a value or values. ˆ A box which contains something. ˆ In most languages, a statement looks like var = expression, where var is a variable and expression

More information

Structure Array 1 / 50

Structure Array 1 / 50 Structure Array A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data. Access data in a structure using dot notation of

More information

IEEE Floating-Point Representation 1

IEEE Floating-Point Representation 1 IEEE Floating-Point Representation 1 x = ( 1) s M 2 E The sign s determines whether the number is negative (s = 1) or positive (s = 0). The significand M is a fractional binary number that ranges either

More information

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Logic is the anatomy of thought. John Locke ( ) This sentence is false. Logic is the anatomy of thought. John Locke (1632 1704) This sentence is false. I know that I know nothing. anonymous Plato (In Apology, Plato relates that Socrates accounts for his seeming wiser than

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 172 / 225 Functions The first thing of the design of algorithms is to divide and conquer. A large and complex problem would be solved by couples

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87 Data Types Java is a strongly-typed 1 programming language. Every variable has a type. Also, every (mathematical) expression has a type. There are two categories of data types: primitive data types, and

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221

1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 1 >> Lecture 3 2 >> 3 >> -- Functions 4 >> Zheng-Liang Lu 169 / 221 Functions Recall that an algorithm is a feasible solution to the specific problem. 1 A function is a piece of computer code that accepts

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 44

More information

Common Errors double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141

Common Errors double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141 Common Errors 2 double area; 3 if (r > 0); 4 area = r r 3.14; 5 System.out.println(area); 6... Zheng-Liang Lu Java Programming 101 / 141 Generating random numbers Example Write a program which generates

More information

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 141 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable.

Scope of Variables. In general, it is not a good practice to define many global variables. 1. Use global to declare x as a global variable. Scope of Variables The variables used in function m-files are known as local variables. Any variable defined within the function exists only for the function to use. The only way a function can communicate

More information

MATLAB TUTORIAL WORKSHEET

MATLAB TUTORIAL WORKSHEET MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent

More information

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

1 class Lecture3 { 2 3 Selections // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137 1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 89 / 137 Flow Controls The basic algorithm (and program) is constituted

More information

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

1 class Lecture3 { 2 3 Selections // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133 1 class Lecture3 { 2 3 "Selections" 4 5 } 6 7 // Keywords 8 if, else, else if, switch, case, default Zheng-Liang Lu Java Programming 88 / 133 Flow Controls The basic algorithm (and program) is constituted

More information

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28 Control Structures Dr. Mihail March 1, 2015 (Dr. Mihail) Control March 1, 2015 1 / 28 Overview So far in this course, MATLAB programs consisted of a ordered sequence of mathematical operations, functions,

More information

Boolean Logic & Branching Lab Conditional Tests

Boolean Logic & Branching Lab Conditional Tests I. Boolean (Logical) Operations Boolean Logic & Branching Lab Conditional Tests 1. Review of Binary logic Three basic logical operations are commonly used in binary logic: and, or, and not. Table 1 lists

More information

switch-case Statements

switch-case Statements switch-case Statements A switch-case structure takes actions depending on the target variable. 2 switch (target) { 3 case v1: 4 // statements 5 break; 6 case v2: 7. 8. 9 case vk: 10 // statements 11 break;

More information

ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory.

ˆ Note that we often make a trade-off between time and space. ˆ Time complexity ˆ Space complexity. ˆ Unlike time, we can reuse memory. ˆ We use O-notation to describe the asymptotic 1 upper bound of complexity of the algorithm. ˆ So O-notation is widely used to classify algorithms by how they respond to changes in its input size. 2 ˆ

More information

McTutorial: A MATLAB Tutorial

McTutorial: A MATLAB Tutorial McGill University School of Computer Science Sable Research Group McTutorial: A MATLAB Tutorial Lei Lopez Last updated: August 2014 w w w. s a b l e. m c g i l l. c a Contents 1 MATLAB BASICS 3 1.1 MATLAB

More information

Introduction to Matlab Programming with Applications

Introduction to Matlab Programming with Applications Introduction to Matlab Programming with Applications Zheng-Liang Lu Department of Computer Science and Information Engineering National Taiwan University Matlab 256 Summer 2015 Class Information Official

More information

AN INTRODUCTION TO MATLAB

AN INTRODUCTION TO MATLAB AN INTRODUCTION TO MATLAB 1 Introduction MATLAB is a powerful mathematical tool used for a number of engineering applications such as communication engineering, digital signal processing, control engineering,

More information

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An. CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Scientific Computing with MATLAB

Scientific Computing with MATLAB Scientific Computing with MATLAB Dra. K.-Y. Daisy Fan Department of Computer Science Cornell University Ithaca, NY, USA UNAM IIM 2012 2 Focus on computing using MATLAB Computer Science Computational Science

More information

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Lisa A. Oberbroeckling Loyola University Maryland loberbroeckling@loyola.edu L. Oberbroeckling (Loyola University) MATLAB 10/07/10

More information

++x vs. x++ We will use these notations very often.

++x vs. x++ We will use these notations very often. ++x vs. x++ The expression ++x first increments the value of x and then returns x. Instead, the expression x++ first returns the value of x and then increments itself. For example, 1... 2 int x = 1; 3

More information

7 Control Structures, Logical Statements

7 Control Structures, Logical Statements 7 Control Structures, Logical Statements 7.1 Logical Statements 1. Logical (true or false) statements comparing scalars or matrices can be evaluated in MATLAB. Two matrices of the same size may be compared,

More information

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An. CSE 10 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An CSE 170 Interacting with MATLAB Instructor: Aijun An Department of Computer Science and Engineering York University aan@cse.yorku.ca Outline Starting MATLAB MATLAB Windows Using the Command Window Some

More information

Introduction to MATLAB for Engineers, Third Edition

Introduction to MATLAB for Engineers, Third Edition PowerPoint to accompany Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 2 Numeric, Cell, and Structure Arrays Copyright 2010. The McGraw-Hill Companies, Inc. This work is

More information

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators. Relational and Logical Operators MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Both operators take on form expression1 OPERATOR expression2 and evaluate to either TRUE (1) or FALSE

More information

Lecture 2: Variables, Vectors and Matrices in MATLAB

Lecture 2: Variables, Vectors and Matrices in MATLAB Lecture 2: Variables, Vectors and Matrices in MATLAB Dr. Mohammed Hawa Electrical Engineering Department University of Jordan EE201: Computer Applications. See Textbook Chapter 1 and Chapter 2. Variables

More information

EGR 111 Introduction to MATLAB

EGR 111 Introduction to MATLAB EGR 111 Introduction to MATLAB This lab introduces the MATLAB help facility, shows how MATLAB TM, which stands for MATrix LABoratory, can be used as an advanced calculator. This lab also introduces assignment

More information

Lesson 3: Basic Programming Concepts

Lesson 3: Basic Programming Concepts 3 ICT Gaming Essentials Lesson 3: Basic Programming Concepts LESSON SKILLS After completing this lesson, you will be able to: Explain the types and uses of variables and operators in game programming.

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Numerical Methods Lecture 1

Numerical Methods Lecture 1 Numerical Methods Lecture 1 Basics of MATLAB by Pavel Ludvík The recommended textbook: Numerical Methods Lecture 1 by Pavel Ludvík 2 / 30 The recommended textbook: Title: Numerical methods with worked

More information

Repetition Structures Chapter 9

Repetition Structures Chapter 9 Sum of the terms Repetition Structures Chapter 9 1 Value of the Alternating Harmonic Series 0.9 0.8 0.7 0.6 0.5 10 0 10 1 10 2 10 3 Number of terms Objectives After studying this chapter you should be

More information

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch Computational Photonics Seminar 02, 30 April 2012 Programming in MATLAB controlling of a program s flow of execution branching loops loop control several programming tasks 1 Programming task 1 Plot the

More information

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order).

Exercise: Sorting 1. ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order). Exercise: Sorting 1 ˆ Let A be any array. ˆ Write a program which outputs the sorted array of A (in ascending order). ˆ For example, A = [5, 4, 1, 2, 3]. ˆ Then the sorted array is [1, 2, 3, 4, 5]. ˆ You

More information

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures Introduction to Octave/Matlab Deployment of Telecommunication Infrastructures 1 What is Octave? Software for numerical computations and graphics Particularly designed for matrix computations Solving equations,

More information

Dr. Iyad Jafar. Adapted from the publisher slides

Dr. Iyad Jafar. Adapted from the publisher slides Computer Applications Lab Lab 5 Programming in Matlab Chapter 4 Sections 1,2,3,4 Dr. Iyad Jafar Adapted from the publisher slides Outline Program design and development Relational operators and logical

More information

Introduction to Engineering gii

Introduction to Engineering gii 25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2.

More information

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133 Scanner Objects It is not convenient to modify the source code and recompile it for a different radius. Reading from the console enables the program to receive an input from the user. A Scanner object

More information

MBI REU Matlab Tutorial

MBI REU Matlab Tutorial MBI REU Matlab Tutorial Lecturer: Reginald L. McGee II, Ph.D. June 8, 2017 MATLAB MATrix LABoratory MATLAB is a tool for numerical computation and visualization which allows Real & Complex Arithmetics

More information

Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

More information

PART 1 PROGRAMMING WITH MATHLAB

PART 1 PROGRAMMING WITH MATHLAB PART 1 PROGRAMMING WITH MATHLAB Presenter: Dr. Zalilah Sharer 2018 School of Chemical and Energy Engineering Universiti Teknologi Malaysia 23 September 2018 Programming with MATHLAB MATLAB Environment

More information

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo Purpose: # Describe the method for constructing algorithms. # Describe an informal language for

More information

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Introduction to Matlab. By: Hossein Hamooni Fall 2014 Introduction to Matlab By: Hossein Hamooni Fall 2014 Why Matlab? Data analytics task Large data processing Multi-platform, Multi Format data importing Graphing Modeling Lots of built-in functions for rapid

More information

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain Introduction to Matlab By: Dr. Maher O. EL-Ghossain Outline: q What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control

More information

9. Elementary Algebraic and Transcendental Scalar Functions

9. Elementary Algebraic and Transcendental Scalar Functions Scalar Functions Summary. Introduction 2. Constants 2a. Numeric Constants 2b. Character Constants 2c. Symbol Constants 2d. Nested Constants 3. Scalar Functions 4. Arithmetic Scalar Functions 5. Operators

More information

Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators Arithmetic Compound Assignment Operators Note that these shorthand operators are not available in languages such as Matlab and R. Zheng-Liang Lu Java Programming 76 / 172 Example 1... 2 int x = 1; 3 System.out.println(x);

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 2 Basic MATLAB Operation Dr Richard Greenaway 2 Basic MATLAB Operation 2.1 Overview 2.1.1 The Command Line In this Workshop you will learn how

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Chapter 3. Set Theory. 3.1 What is a Set?

Chapter 3. Set Theory. 3.1 What is a Set? Chapter 3 Set Theory 3.1 What is a Set? A set is a well-defined collection of objects called elements or members of the set. Here, well-defined means accurately and unambiguously stated or described. Any

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Dr./ Ahmed Nagib Mechanical Engineering department, Alexandria university, Egypt Spring 2017 Chapter 4 Decision making and looping functions (If, for and while functions) 4-1 Flowcharts

More information

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems SCHOOL OF ENGINEERING & BUILT ENVIRONMENT Mathematics Numbers & Number Systems Introduction Numbers and Their Properties Multiples and Factors The Division Algorithm Prime and Composite Numbers Prime Factors

More information

Chapter 7: Programming in MATLAB

Chapter 7: Programming in MATLAB The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Computer Programming (ECIV 2302) Chapter 7: Programming in MATLAB 1 7.1 Relational and Logical Operators == Equal to ~=

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4. Introduction to Visual Basic and Visual C++ Arithmetic Expression Lesson 4 Calculation I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Arithmetic Expression Using Arithmetic Expression Calculations

More information

Dr Richard Greenaway

Dr Richard Greenaway SCHOOL OF PHYSICS, ASTRONOMY & MATHEMATICS 4PAM1008 MATLAB 3 Creating, Organising & Processing Data Dr Richard Greenaway 3 Creating, Organising & Processing Data In this Workshop the matrix type is introduced

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks MATLAB Basics Stanley Liang, PhD York University Configure a MATLAB Package Get a MATLAB Student License on Matworks Visit MathWorks at https://www.mathworks.com/ It is recommended signing up with a student

More information

EE 301 Signals & Systems I MATLAB Tutorial with Questions

EE 301 Signals & Systems I MATLAB Tutorial with Questions EE 301 Signals & Systems I MATLAB Tutorial with Questions Under the content of the course EE-301, this semester, some MATLAB questions will be assigned in addition to the usual theoretical questions. This

More information

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix. MATLAB Tutorial 1 1 Department of Mathematics and Statistics, The University of New Mexico, Albuquerque, NM 87131 August 28, 2016 Contents: 1. Scalars, Vectors, Matrices... 1 2. Built-in variables, functions,

More information

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Using Monte Carlo to Estimate π using Buffon s Needle Problem An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Here s the problem (in a simplified form). Suppose

More information

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control Chapter 2 Control, Quick Overview Control Selection Selection Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing 1 Python if statement

More information

Relational and Logical Operators

Relational and Logical Operators Relational and Logical Operators Relational Operators Relational operators are used to represent conditions (such as space 0 in the water tank example) Result of the condition is either true or false In

More information

Programming for Experimental Research. Flow Control

Programming for Experimental Research. Flow Control Programming for Experimental Research Flow Control FLOW CONTROL In a simple program, the commands are executed one after the other in the order they are typed. Many situations require more sophisticated

More information

Matlab- Command Window Operations, Scalars and Arrays

Matlab- Command Window Operations, Scalars and Arrays 1 ME313 Homework #1 Matlab- Command Window Operations, Scalars and Arrays Last Updated August 17 2012. Assignment: Read and complete the suggested commands. After completing the exercise, copy the contents

More information

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment What is MATLAB? MATLAB PROGRAMMING Stands for MATrix LABoratory A software built around vectors and matrices A great tool for numerical computation of mathematical problems, such as Calculus Has powerful

More information

The return Statement

The return Statement The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return

More information

CME 192: Introduction to Matlab

CME 192: Introduction to Matlab CME 192: Introduction to Matlab Matlab Basics Brett Naul January 15, 2015 Recap Using the command window interactively Variables: Assignment, Identifier rules, Workspace, command who and whos Setting the

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Introduction MATLAB is an interactive package for numerical analysis, matrix computation, control system design, and linear system analysis and design available on most CAEN platforms

More information

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics 400 lecture note #4 [Ch 6] Set Theory 1. Basic Concepts and Definitions 1) Basics Element: ; A is a set consisting of elements x which is in a/another set S such that P(x) is true. Empty set: notated {

More information

Chapter 4: Control Structures I (Selection)

Chapter 4: Control Structures I (Selection) Chapter 4: Control Structures I (Selection) 1 Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover

More information

Scilab Programming. The open source platform for numerical computation. Satish Annigeri Ph.D.

Scilab Programming. The open source platform for numerical computation. Satish Annigeri Ph.D. Scilab Programming The open source platform for numerical computation Satish Annigeri Ph.D. Professor, Civil Engineering Department B.V.B. College of Engineering & Technology Hubli 580 031 satish@bvb.edu

More information

Relational and Logical Statements

Relational and Logical Statements Relational and Logical Statements Relational Operators in MATLAB A operator B A and B can be: Variables or constants or expressions to compute Scalars or arrays Numeric or string Operators: > (greater

More information

Example. Write a program which generates 2 random integers and asks the user to answer the math expression.

Example. Write a program which generates 2 random integers and asks the user to answer the math expression. Generating random numbers Example Write a program which generates 2 random integers and asks the user to answer the math expression. For example, the program shows 2 + 5 =? If the user answers 7, then

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB 1 Introduction to MATLAB A Tutorial for the Course Computational Intelligence http://www.igi.tugraz.at/lehre/ci Stefan Häusler Institute for Theoretical Computer Science Inffeldgasse

More information

Computers in Engineering COMP 208 Repetition and Storage Michael A. Hawker. Repetition. A Table of Values 9/20/2007

Computers in Engineering COMP 208 Repetition and Storage Michael A. Hawker. Repetition. A Table of Values 9/20/2007 Computers in Engineering COMP 208 Repetition and Storage Michael A. Hawker Repetition To fully take advantage of the speed of a computer, we must be able to instruct it to do a lot of work The program

More information

Matlab Basics Lecture 2. Juha Kuortti October 28, 2017

Matlab Basics Lecture 2. Juha Kuortti October 28, 2017 Matlab Basics Lecture 2 Juha Kuortti October 28, 2017 1 Lecture 2 Logical operators Flow Control 2 Relational Operators Relational operators are used to compare variables. There are 6 comparison available:

More information

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline MATLAB Tutorial EE351M DSP Created: Thursday Jan 25, 2007 Rayyan Jaber Modified by: Kitaek Bae Outline Part I: Introduction and Overview Part II: Matrix manipulations and common functions Part III: Plots

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

More information

APPM 2460: Week Three For, While and If s

APPM 2460: Week Three For, While and If s APPM 2460: Week Three For, While and If s 1 Introduction Today we will learn a little more about programming. This time we will learn how to use for loops, while loops and if statements. 2 The For Loop

More information

APPM 2460 Matlab Basics

APPM 2460 Matlab Basics APPM 2460 Matlab Basics 1 Introduction In this lab we ll get acquainted with the basics of Matlab. This will be review if you ve done any sort of programming before; the goal here is to get everyone on

More information

Introduction to MATLAB 7 for Engineers

Introduction to MATLAB 7 for Engineers PowerPoint to accompany Introduction to MATLAB 7 for Engineers William J. Palm III Chapter 2 Numeric, Cell, and Structure Arrays Copyright 2005. The McGraw-Hill Companies, Inc. Permission required for

More information

Computational Finance

Computational Finance Computational Finance Introduction to Matlab Marek Kolman Matlab program/programming language for technical computing particularly for numerical issues works on matrix/vector basis usually used for functional

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

Finding, Starting and Using Matlab

Finding, Starting and Using Matlab Variables and Arrays Finding, Starting and Using Matlab CSC March 6 &, 9 Array: A collection of data values organized into rows and columns, and known by a single name. arr(,) Row Row Row Row 4 Col Col

More information

Computer Programming in MATLAB

Computer Programming in MATLAB Computer Programming in MATLAB Prof. Dr. İrfan KAYMAZ Atatürk University Engineering Faculty Department of Mechanical Engineering What is a computer??? Computer is a device that computes, especially a

More information

1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207

1 class Lecture4 { 2 3 Loops / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207 1 class Lecture4 { 2 3 "Loops" 4 5 } 6 7 / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207 Loops A loop can be used to make a program execute statements repeatedly without having

More information

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

More information

Computer Programming C++ (wg) CCOs

Computer Programming C++ (wg) CCOs Computer Programming C++ (wg) CCOs I. The student will analyze the different systems, and languages of the computer. (SM 1.4, 3.1, 3.4, 3.6) II. The student will write, compile, link and run a simple C++

More information

Introduction to MATLAB

Introduction to MATLAB 58:110 Computer-Aided Engineering Spring 2005 Introduction to MATLAB Department of Mechanical and industrial engineering January 2005 Topics Introduction Running MATLAB and MATLAB Environment Getting help

More information

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB: Contents VARIABLES... 1 Storing Numerical Data... 2 Limits on Numerical Data... 6 Storing Character Strings... 8 Logical Variables... 9 MATLAB S BUILT-IN VARIABLES AND FUNCTIONS... 9 GETTING HELP IN MATLAB...

More information